package com.wcl.demo.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.poi.PoiResult;
import com.wcl.demo.R;
import com.wcl.demo.ui.activity.iteminterface.OnLocationItemClickListener;

import java.security.Policy;
import java.util.List;

/**
 * Created by dllo on 15/12/2.
 */
public class LocationAdapter extends RecyclerView.Adapter<LocationAdapter.LocationAdapterViewHolder> {

    private List<PoiInfo> results;
    private OnLocationItemClickListener listener;

    public void setLocationClickListener(OnLocationItemClickListener listener) {
        this.listener = listener;
    }

    public void addData(List<PoiInfo> results) {
        this.results = results;
        notifyDataSetChanged();
    }

    class LocationAdapterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView textView;
        private LinearLayout location_item_linearlayout;
        private int position;

        public LocationAdapterViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.location_nametextView);
            location_item_linearlayout = (LinearLayout) itemView.findViewById(R.id.location_item_linearlayout);
            location_item_linearlayout.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (results != null && results.size() > 0) {
                PoiInfo info = results.get(position);
                if (listener != null) {
                    listener.onClickLocationListener(info);
                }
            }
        }
    }

    @Override
    public int getItemCount() {
        return results != null && results.size() > 0 ? results.size() : 0;
    }

    @Override
    public LocationAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.activity_location_item, null);
        return new LocationAdapterViewHolder(view);
    }

    @Override
    public void onBindViewHolder(LocationAdapterViewHolder holder, int position) {
        if (results != null) {
            holder.textView.setText(results.get(position).name);
        }
        holder.position = position;
    }

}
